#!/bin/sh

revision="offset v0.2.4 (2023-06-27 by MIBonk & MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
	echo -ne "\n$ME-$thisname---->\n" >> $LOG
fi

if [ -f $TMP/reboot.mib ]; then
	echo "Reboot running..."
	return 2> /dev/null
fi

function BLOCKS {
hex_value1="$OFFSETPART2"
hex_value2="$IFSSTART" #start offset of ifs-root on rcc / skip=1344
decimal_value1=$(echo "ibase=16; $hex_value1" | $BC) 2>> $LOG
decimal_value2=$(echo "ibase=16; $hex_value2" | $BC) 2>> $LOG
result=$((decimal_value1 + decimal_value2))
SKIPBLOCKS=$((result / 4096)) # calculate skips needed to get to start of ifs-root-stage2
}

MIBCAP=3
. $thisdir/../config/MIBCHECK

case $1 in
	-log) { #get offset and size of ifs-root-stage2
		if [[ -f /net/rcc/usr/bin/flashlock ]]; then
			OFFSETPART2=$($FLASHLOCK | grep "3 image type=2(IFS     )" | $CUT -c27-32) 2>> $LOG
			result=$(echo "ibase=16; $OFFSETPART2" | $BC) 2>> $LOG
			# file size ifs-root-part2 with fixed values and compare to flashlock output
			STAGE2SIZE="$($FLASHLOCK | grep $OFFSETPART2)"
			STAGE2SIZE=$(echo $STAGE2SIZE | awk -v FS="(" '{print $3}' | awk -v FS=")" '{print $1}') 2>> $LOG # get ifs-root-stage2 size in bytes
			STAGE2SIZE="00"$(echo "obase=16; $STAGE2SIZE" | $BC) 2>> $LOG
			STAGE2SIZE=$(echo $STAGE2SIZE | $SED "s/.\{2\}/&:/g" | awk '{print toupper($0)}') 2>> $LOG
			# Remove colons and convert endianess
			STAGE2SIZE=$(echo $STAGE2SIZE|cut -d':' -f4)$(echo $STAGE2SIZE|cut -d':' -f3)$(echo $STAGE2SIZE|cut -d':' -f2)$(echo $STAGE2SIZE|cut -d':' -f1)
			IFSSTART="0"
			BLOCKS
		elif [[ -f $BACKUPFOLDER/$MUVERSION-ifs-root-part2-OFFSET.txt ]] && grep -q OFFSETPART2 $BACKUPFOLDER/$MUVERSION-ifs-root-part2-OFFSET.txt && grep -q STAGE2SIZE $BACKUPFOLDER/$MUVERSION-ifs-root-part2-OFFSET.txt; then
			echo -ne "Reading OFFSETPART2 and STAGE2SIZE from backup\n" | $TEE -i -a $LOG
			OFFSETPART2=$(grep OFFSETPART2 $BACKUPFOLDER/$MUVERSION-ifs-root-part2-OFFSET.txt | cut -c18-23) 2>> $LOG
			STAGE2SIZE=$(grep STAGE2SIZE $BACKUPFOLDER/$MUVERSION-ifs-root-part2-OFFSET.txt | cut -c13-20) 2>> $LOG
			IFSSTART="0"
			BLOCKS
		else
			echo -ne "Searching ifs-root-stage2.ifs offset\nIt will take about 2 minutes, please wait...\n" | $TEE -i -a $LOG
			OFFSETPART2=$($DD bs=4096 skip=1344 count=6144 if=$fs0p0 2> /dev/null | $XXD -u | grep 'EB7E FF00 0100 08' | $CUT -c 3-8) 2>> $LOG
			IFSSTART="540000"
			BLOCKS
			OFFSETPART2=$(echo "obase=16; $result" | $BC | awk '{printf("%06s\n", $0)}') 2>> $LOG
			echo -ne "Calculating ifs-root-stage2.ifs size.\nIt will take about 1 minute, please wait...\n" | $TEE -i -a $LOG
			STAGE2SIZE=$($DD bs=4096 skip=$SKIPBLOCKS count=1 if=$fs0p0 2> /dev/null | $DD bs=4 skip=9 count=1 2> /dev/null | $XXD -u -ps ) 2>> $LOG
		fi
		
		echo -ne "ifs-root-stage2.ifs offset: 0x00$OFFSETPART2\n" | $TEE -i -a $LOG
		echo -ne "ifs-root-stage2.ifs size: $STAGE2SIZE\n" | $TEE -i -a $LOG
		return 2> /dev/null
	};;

	*) { # help or unknown parameter ------------------------------
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION]"
		echo ""
		echo "Options:"
		echo "        -log     find offset and size of ifs-root.ifs in rcc flash"
		echo "        --help   show this help"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
	};;
esac
exit 0